#include "contig.h"

Contig::Contig(QPointer<SageVis> sageVisPointer, MultiGeneRegionVisWidget* parentWidget, QGraphicsItem *parent ) :
    VisBaseContainer( sageVisPointer, dynamic_cast<VisBaseWidget*> (parentWidget), parent)
{
    init();
}

Contig::Contig(QPointer<ContigDataPacket> dataPacket ,QPointer<SageVis> sageVisPointer, MultiGeneRegionVisWidget* parentWidget, QGraphicsItem *parent ) :
    VisBaseContainer( sageVisPointer, dynamic_cast<VisBaseWidget*> (parentWidget), parent)
{
    setDataPacket( dataPacket );
    init();
}

void Contig::init()
{
    setCursor( Qt::ArrowCursor );//make sure arrow cursor is shown over contig
    //setAcceptsHoverEvents(true);//when hovering over a contig, make it more prominent
}

//------------------------------SETTERS-------------------------------
void Contig::setDataPacket(QPointer<ContigDataPacket> dataPacket)
{
   this->dataPacket = dataPacket;
}

//NOTE- later may want to change this so that I am not
void Contig::addGenomeElement( GenomeElement* genomeElement )
{
    //forgeConnection(this, genomeElement, HEIGHT_CONNECTION );//genome elements should be same height as parent

    genomeElements.append( genomeElement );
    elements.append( genomeElement );
}


//-----------------SIZE AND SHAPE FUNCTIONS----------------------------

//void Contig::setGeometry(const QRectF &geom)
//{
//    prepareGeometryChange();
//    QGraphicsWidget::setGeometry(geom);
//    setPos(geom.topLeft());
//}


//QRectF Contig::boundingRect() const
//{
//    return QRectF(QPointF(0,0), geometry().size());
//}


//QPainterPath Contig::shape() const
//{
//    QPainterPath path;
//    path.addRect(boundingRect());
//    return path;
//}


//QSizeF Contig::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
//{
//    switch (which) {
//    case Qt::MinimumSize:
//        return QSizeF(500, 10);
//    case Qt::PreferredSize:
//        return QSizeF(500, 20);
//    case Qt::MaximumSize:
//        return QSizeF(500, 30);
//    default:
//        break;
//    }
//    return constraint;
//}

//-----------------------------------PROCESS USER INPUT-------------------------------------
void Contig::processHoverEvent(QGraphicsSceneHoverEvent *event)
{
    float newHeight = geometry().height();
    if( !hovering )//not hovering now, will soon
    {
        newHeight *= 3.0;//increase size if hovering
    }
    else
    {
        newHeight /= 3.0;//decrease size if no longer hovering
    }

    setHeight( newHeight );//set the height

    qDebug() << "contig processes hover event" << endl;

    super::processHoverEvent(event); //call base class to ensure hover event goes through
    emit( recomputeLayoutSignal() ); //with height change, must recompute layout
}

void Contig::receiveHoverEvent(QGraphicsSceneHoverEvent *event)
{
    qDebug() << "contig receive's hover event" << endl;
    super::receiveHoverEvent(event); //call base class to ensure hover event goes through
    emit( recomputeLayoutSignal() ); //with height change, must recompute layout
}

void Contig::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    //----prevent compiler warnings by declaring what I am not using---
    Q_UNUSED(widget);
    Q_UNUSED(option);

    //---set color---
    QColor backgroundColor;

    if( hovering )
    {
        backgroundColor.setRgb(90.0, 90.0, 90.0);
    }
    else
        backgroundColor.setRgb(60.0, 60.0, 60.0);


    //---paint background rect---
    painter->setBrush(backgroundColor);
    painter->setPen( Qt::NoPen );
    painter->drawRoundedRect(boundingRect(), .25*boundingRect().height(), .25*boundingRect().height() );
}

